package utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;

import model.NastaveniUlohy;
import model.UlohyEnum;
import model.Zadani;

/**
 * Obecne pouzivane nemenici se objekty
 * @author schovjan
 * @date Jan 10, 2012
 */
public class Nastroje {

    // pismo
    public static Font fMega = new Font("Arial", Font.BOLD, 120);
    public static Font fVelky = new Font("Arial", Font.BOLD, 34);
    public static Font fVetsi = new Font("Arial", Font.BOLD, 26);
    public static Font fStredni = new Font("Arial", Font.BOLD, 16);
    public static Font fMaly = new Font("Arial", Font.BOLD, 12);
    public static Color cZluta = new Color(250, 250, 20);
    public static Color cZelena = new Color(0, 255, 50);
    public static Color cCervena = new Color(255, 0, 50);
    private static String minulyObr = "";


    /**
     * Pri spusteni program zkontroluje, zda existuje slozka aplikace s daty a obrazky,
     * pokud neexistuje, tak ji vytvori.
     */
    public static void inicializujSlozkuAplikace() {
	// overeni, ze existuje properties s nastavenim, pokud ne tak jej vytvoří
	File f = new File(getAdresaAppSlozky());
	if (!f.exists()) {
	    f.mkdir();
	}
    }


    /**
     * Vrati nahodne vybrany obrazek ze slozky "obr"
     * Stara se o to, aby se jeden obrazek neopakoval dvakrat po sobe.
     * @return
     * @throws IOException
     */
    public static BufferedImage getNahodnyObrazek() throws Exception {
	// new ImageIcon(this.getClass().getResource(Constants.URL_IMG_GUI+"icon.png")));
	File slozka = new File(getAdresaAppSlozky() + "obr");
	int nahoda = new Random().nextInt(slozka.listFiles().length);
	if (!slozka.listFiles()[nahoda].getName().equals(minulyObr)) {
	    minulyObr = slozka.listFiles()[nahoda].getName();
	    return ImageIO.read(slozka.listFiles()[nahoda]);
	} else {
	    return getNahodnyObrazek();
	}
    }


    /**
     * Vraci seznam zadani
     * @param nastaveni
     * @return
     * @throws Exception
     */
    public static List<Zadani> getZadani(NastaveniUlohy nastaveni) throws Exception {
	List<Zadani> result = new ArrayList<Zadani>();
	BufferedReader in = new BufferedReader(
				new InputStreamReader(
					new FileInputStream(getAdresaAppSlozky()
						+ "txt/vyjmenovana_slova.txt"), "UTF8"));
	String str;
	boolean nacitam = false;
	while ((str = in.readLine()) != null) {
	    // komentar se nenacita
	    if (str.startsWith("//")) {
		nacitam = false;
	    }
	    // nacitat pouze retezce dle zadani
	    if (str.charAt(0) == ';') {
		if ((str.contains(UlohyEnum.VS_B.toString()) && nastaveni.getZadani().contains(UlohyEnum.VS_B))
			|| (str.contains(UlohyEnum.VS_L.toString()) && nastaveni.getZadani().contains(UlohyEnum.VS_L))
			|| (str.contains(UlohyEnum.VS_M.toString()) && nastaveni.getZadani().contains(UlohyEnum.VS_M))
			|| (str.contains(UlohyEnum.VS_P.toString()) && nastaveni.getZadani().contains(UlohyEnum.VS_P))
			|| (str.contains(UlohyEnum.VS_S.toString()) && nastaveni.getZadani().contains(UlohyEnum.VS_S))
			|| (str.contains(UlohyEnum.VS_V.toString()) && nastaveni.getZadani().contains(UlohyEnum.VS_V))
			|| (str.contains(UlohyEnum.VS_Z.toString()) && nastaveni.getZadani().contains(UlohyEnum.VS_Z))) {
		    nacitam = true;
		} else {
		    nacitam = false;
		}
		continue;
	    }
	    if (nacitam) {
		Zadani slovo = new Zadani();
		int i = str.indexOf('/');
		slovo.setOriginal(str.substring(0, i));
		slovo.setUpravene(str.substring(i + 1));
		result.add(slovo);
	    }
	}
	in.close();
	return result;
    }
    
    
    /**
     * Vraci adresu ke slozce aplikace
     * @return
     */
    private static String getAdresaAppSlozky() {
	return System.getProperty("user.home") + "/.skolnik/";
    }
}
